//package com.jh.paymentchannel.service;
//
//import cn.jh.common.utils.Constss;
//import cn.jh.common.utils.ResultWrap;
//import com.google.common.base.Strings;
//import com.google.common.collect.Lists;
//import com.jh.paymentchannel.business.ChannelBusiness;
//import com.jh.paymentchannel.payment.PaymentException;
//import com.jh.paymentchannel.pojo.freeQuick.*;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.domain.Page;
//import org.springframework.data.domain.PageRequest;
//import org.springframework.data.domain.Pageable;
//import org.springframework.data.domain.Sort;
//import org.springframework.web.bind.annotation.PostMapping;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RequestParam;
//import org.springframework.web.bind.annotation.RestController;
//
//import javax.servlet.http.HttpServletRequest;
//import java.math.BigDecimal;
//import java.util.Date;
//import java.util.List;
//
//import static org.springframework.web.bind.annotation.RequestMethod.POST;
//
//
//// 通道管理控制器（主要用于后台管理）
//@RestController
//public class PaymentChannelController {
//
//
//    private final ChannelBusiness channelBusiness;
//
//    @Autowired
//    public PaymentChannelController(ChannelBusiness channelBusiness) {
//        this.channelBusiness = channelBusiness;
//    }
//
//    /**
//     * 获取可用通道
//     */
//    @RequestMapping(value = "/smartpayment/pay/channels", method = POST)
//    public Object findPaymentChannel() {
//        ChannelCode[] codes = ChannelCode.values();
//        List<PaymentChannel> channelList = Lists.newLinkedList();
//        for (ChannelCode scc: codes) {
//            channelList.add(PaymentChannel.init(scc.getDesc(), scc.getValue()));
//        }
//        return ResultWrap.init(Constss.SUCCESS, "成功", channelList);
//    }
//
//    /**
//     * 支付通道查询
//     **/
//    @RequestMapping(value = "/smartpayment/pay/channel/all")
//    public Object queryAllPaymentChannel(HttpServletRequest request) {
//        List<PaymentChannel> channels = channelBusiness.queryAllPaymentChannels();
//
//        return ResultWrap.init(Constss.SUCCESS, "成功", channels);
//    }
//
//    /**
//     * 外放通道查询
//     **/
//    @RequestMapping(value = "/smartpayment/platform/pay/channel/all")
//    public Object queryAllPCPaymentChannel(
//            //外放通道
//            @RequestParam(value = "channel_code", required = false) String channelCode,
//            //类型
//            @RequestParam(value = "channel_type", required = false) String channelType
//    ) {
//        channelCode = Strings.emptyToNull(channelCode);
//        channelType = Strings.emptyToNull(channelType);
//        List<PaymentChannel> channels = channelBusiness.queryAllPaymentChannels(channelCode, channelType);
//
//        return ResultWrap.init(Constss.SUCCESS, "成功", channels);
//    }
//
//    @PostMapping(value = "/smartpayment/pay/channel/code")
//    public Object queryPaymentChannelByChannelcode(
//            @RequestParam(value = "channel_code") String channelCode) {
//
//
//        PaymentChannel paymentChannel = channelBusiness.queryPaymentChannelByChannelCode(channelCode);
//
//        if (paymentChannel != null) {
//
//            return ResultWrap.init(Constss.SUCCESS, "成功", paymentChannel);
//        } else {
//            return ResultWrap.init(Constss.FALIED, "失败", "");
//        }
//    }
//
//
//    /**
//     * 支付通道信息删除
//     **/
//    @PostMapping(value = "/smartpayment/pay/channel/del")
//    public Object deletePaymentChannel(
//            @RequestParam(value = "channel_code") String channelCode) {
//
//        try {
//            channelBusiness.delPaymentChannelByChannelcode(channelCode);
//            return ResultWrap.init(Constss.SUCCESS, "成功", "");
//        } catch (Exception e) {
//
//            return ResultWrap.init(Constss.FALIED, "失败", "");
//
//        }
//
//    }
//
//    /**
//     * 支付通道信息编辑
//     */
//    @PostMapping(value = "/smartpayment/pay/channel/edit")
//    public Object editPaymentChannel(PaymentChannelDTO channelDTO, HttpServletRequest request) {
//        try {
//            PaymentChannel channel = channelBusiness.editPaymentChannel(channelDTO);
//            if (channel == null) {
//                return ResultWrap.init(Constss.FALIED, "失败", "");
//            }
//            return ResultWrap.init(Constss.SUCCESS, "成功", channel);
//        } catch (PaymentException ex) {
//            return ResultWrap.init(Constss.FALIED, ex.getMessage(), "");
//        } catch (Exception e) {
//            return ResultWrap.init(Constss.FALIED, "失败", "");
//        }
//    }
//
//    /**
//     * 支付通道信息添加更新
//     **/
//    @PostMapping(value = "/smartpayment/pay/channel/new")
//    public Object updatePaymentChannel(
//            @RequestParam(value = "channel_code") String channelCode,
//            @RequestParam(value = "channel_name") String channelName,
//            @RequestParam(value = "channel_type") String channelType,
//            @RequestParam(value = "real_pay_type", required = false, defaultValue = "0") String realPayType
//    ) {
//
//
//        PaymentChannel paymentChannel = channelBusiness.queryPaymentChannelByChannelCode(channelCode);
//        if (paymentChannel == null) {
//            paymentChannel = new PaymentChannel();
//            paymentChannel.setChannelCode(channelCode);
//            paymentChannel.setChannelName(channelName);
//            paymentChannel.setChannelType(channelType);
//            paymentChannel.setCreateTime(new Date());
//            paymentChannel.setUpdateTime(new Date());
//            paymentChannel.setRealPayType(realPayType);
//        } else {
//            paymentChannel.setChannelCode(channelCode);
//            paymentChannel.setChannelName(channelName);
//            paymentChannel.setChannelType(channelType);
//            paymentChannel.setRealPayType(realPayType);
//            paymentChannel.setUpdateTime(new Date());
//        }
//
//        try {
//            channelBusiness.createPaymentChannel(paymentChannel);
//
//            return ResultWrap.init(Constss.SUCCESS, "成功", paymentChannel);
//        } catch (Exception e) {
//            return ResultWrap.init(Constss.FALIED, "失败", "");
//        }
//
//
//    }
//
//
//    /**
//     * 新增/更新实际支付的渠道
//     */
//    @PostMapping(value = "/smartpayment/channelroute/status/update")
//    public Object updateChannelRouteStatus(
//            @RequestParam(value = "real_channel_code") String realChannelCode,
//            @RequestParam(value = "channel_status") String channelStatus
//    ) {
//
//        try {
//            channelBusiness.updateChannelRouteStatus(realChannelCode, channelStatus);
//
//            return ResultWrap.init(Constss.SUCCESS, "成功", "");
//        } catch (Exception e) {
//            return ResultWrap.init(Constss.FALIED, "失败", "");
//        }
//
//    }
//
//
//    /**
//     * 新增/更新实际支付的渠道
//     */
//    @PostMapping(value = "/smartpayment/pay/realchannel/new")
//    public Object addRealChannel(
//            @RequestParam(value = "real_channel_code") String realChannelCode,
//            @RequestParam(value = "real_channel_name") String realChannelName,
//            @RequestParam(value = "rate") BigDecimal rate,
//            @RequestParam(value = "extra_fee", required = false, defaultValue = "0") BigDecimal extraFee,
//            @RequestParam(value = "channel_status", required = false, defaultValue = "0") String channelStatus,
//            @RequestParam(value = "settlement_id", required = false) String settlementid,
//            @RequestParam(value = "ratio", required = false, defaultValue = "1") int ratio,
//            @RequestParam(value = "real_pay_type", required = false, defaultValue = "0") String realPayType
//    ) {
//
//        RealChannel realChannel = channelBusiness.queryRealChannel(realChannelCode);
//        if (realChannel == null) {
//
//            realChannel = new RealChannel();
//            realChannel.setCreateTime(new Date());
//            realChannel.setExtraFee(extraFee);
//            realChannel.setRate(rate);
//            realChannel.setRealChannelCode(realChannelCode);
//            realChannel.setRealChannelName(realChannelName);
//            realChannel.setStatus(channelStatus);
//            realChannel.setSettlementId(settlementid);
//            realChannel.setRatio(ratio);
//            realChannel.setRealPayType(realPayType);
//            realChannel.setUpdateTime(new Date());
//
//        } else {
//
//            realChannel.setExtraFee(extraFee);
//            realChannel.setRate(rate);
//            realChannel.setRealChannelCode(realChannelCode);
//            realChannel.setRealChannelName(realChannelName);
//            realChannel.setStatus(channelStatus);
//            realChannel.setSettlementId(settlementid);
//            realChannel.setRatio(ratio);
//            realChannel.setRealPayType(realPayType);
//            realChannel.setUpdateTime(new Date());
//
//        }
//
//
//        try {
//            channelBusiness.saveRealChannel(realChannel);
//            return ResultWrap.init(Constss.SUCCESS, "成功", realChannel);
//        } catch (Exception e) {
//            return ResultWrap.init(Constss.FALIED, "失败", "");
//        }
//
//
//    }
//
//
//    /**
//     * 支付通道信息删除
//     **/
//    @PostMapping(value = "/smartpayment/pay/realchannel/del")
//    public Object deletePaymentRealChannel(
//            @RequestParam(value = "real_channel_code") String realChannelCode
//    ) {
//
//        try {
//            channelBusiness.delRealChannel(realChannelCode);
//            return ResultWrap.init(Constss.SUCCESS, "成功", "");
//        } catch (Exception e) {
//            return ResultWrap.init(Constss.FALIED, "失败", "");
//        }
//
//    }
//
//    @PostMapping(value = "/smartpayment/pay/realchannel/all")
//    public Object queryAllPaymentRealChannel() {
//
//        List<RealChannel> channels = channelBusiness.queryAllRealchannels();
//
//        return ResultWrap.init(Constss.SUCCESS, "成功", channels);
//    }
//
//    /**
//     * 上游通道查询
//     * pc
//     **/
//    @PostMapping(value = "/smartpayment/platform/pay/realchannel/all")
//    public Object queryAllPCPaymentRealChannel(
//            //上游通道
//            @RequestParam(value = "real_channel_code", required = false) String realChannelCode,
//            //类型
//            @RequestParam(value = "channel_type", required = false) String channelType
//    ) {
//
//        List<RealChannel> channels = channelBusiness.queryAllPCPaymentRealChannels(realChannelCode, channelType);
//
//        return ResultWrap.init(Constss.SUCCESS, "成功", channels);
//    }
//
//
//    /**
//     * 查询所有支付通道的路由
//     **/
//    @PostMapping(value = "/smartpayment/pay/route/query")
//    public Object queryChannelRoute(
//            @RequestParam(value = "merchant_id", required = false) String merchantId,
//            @RequestParam(value = "page", defaultValue = "0", required = false) int page, //
//            @RequestParam(value = "size", defaultValue = "20", required = false) int size,
//            @RequestParam(value = "order", defaultValue = "DESC", required = false) Sort.Direction direction, //
//            @RequestParam(value = "sort", defaultValue = "createTime", required = false) String sortProperty) {
//
//
//        Pageable pageable = new PageRequest(page, size, new Sort(direction, sortProperty));
//
//        Page<PaymentChannelRoute> channelRoutes = channelBusiness.pagePaymentChannelRoute(merchantId, pageable);
//
//        return ResultWrap.init(Constss.SUCCESS, "成功", channelRoutes);
//
//    }
//
//
//    /**
//     * 新增或者更新商户的通道到支付通道的路由
//     **/
//    @PostMapping(value = "/smartpayment/pay/route/new")
//    public Object addChannelRoute(
//            @RequestParam(value = "merchant_id") String merchantId,
//            @RequestParam(value = "channel_code") String channelCode,
//            @RequestParam(value = "channel_name", required = false) String channelName,
//            @RequestParam(value = "real_channel_code") String realChannelCode,
//            @RequestParam(value = "real_channel_name", required = false) String realChannelName,
//            @RequestParam(value = "ratio", required = false, defaultValue = "1") int ratio){
//
//
//        List<PaymentChannelRoute> paymentChannelRoutes =
//                channelBusiness.queryPaymentChannelRouteByChannelCode(merchantId, channelCode);
//        PaymentChannelRoute paymentChannelRoute;
//        if (paymentChannelRoutes == null || paymentChannelRoutes.size() == 0) {
//
//
//            paymentChannelRoute = new PaymentChannelRoute();
//            paymentChannelRoute.setChannelCode(channelCode);
//            paymentChannelRoute.setChannelName(channelName);
//            paymentChannelRoute.setCreateTime(new Date());
//            paymentChannelRoute.setMerchantId(merchantId);
//            paymentChannelRoute.setRealChannelCode(realChannelCode);
//            paymentChannelRoute.setRealChannelName(realChannelName);
//            paymentChannelRoute.setStatus("0");
//            paymentChannelRoute.setUpdateTime(new Date());
//            paymentChannelRoute.setRatio(ratio);
//        } else {
//            paymentChannelRoute = paymentChannelRoutes.get(0);
//            paymentChannelRoute.setChannelCode(channelCode);
//            paymentChannelRoute.setChannelName(channelName);
//            paymentChannelRoute.setMerchantId(merchantId);
//            paymentChannelRoute.setRealChannelCode(realChannelCode);
//            paymentChannelRoute.setRealChannelName(realChannelName);
//            paymentChannelRoute.setUpdateTime(new Date());
//            paymentChannelRoute.setStatus("0");
//            paymentChannelRoute.setRatio(ratio);
//
//        }
//
//        try {
//            channelBusiness.createPaymentChannelRoute(paymentChannelRoute);
//            return ResultWrap.init(Constss.SUCCESS, "成功", paymentChannelRoute);
//        } catch (Exception e) {
//            return ResultWrap.init(Constss.FALIED, "失败", "");
//        }
//    }
//
//
//    /**
//     * 删除支付通道的路由
//     **/
//    @PostMapping(value = "/smartpayment/pay/route/del")
//    public Object delChannelRoute(@RequestParam(value = "id") long id){
//
//        try {
//            channelBusiness.delPaymentChannelRoute(id);
//            return ResultWrap.init(Constss.SUCCESS, "成功", "");
//        } catch (Exception e) {
//
//            return ResultWrap.init(Constss.FALIED, "失败", "");
//
//        }
//    }
//
//
//}
